﻿#include "FITKRadiossDataMaterialElasticAdaptor.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"
//RadiossData
#include "FITK_Component/FITKRadiossData/FITKMaterialElastic.h"

namespace Interface
{
    bool FITKRadiossDataMaterialElasticAdaptor::adaptR()
    {
        _elasticData = dynamic_cast<Radioss::FITKMaterialElastic*>(_dataObj);
        if (_reader == nullptr || _elasticData == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;

        double density = -1;
        double youngsModulus = -1;
        double poissonsRatio = -1;
        std::string describe{};
        if (!h5tools->readGroupDouble(*_h5Group, "Density", &density)) return false;
        if (!h5tools->readGroupDouble(*_h5Group, "YoungsModulus", &youngsModulus)) return false;
        if (!h5tools->readGroupDouble(*_h5Group, "PoissonsRatio", &poissonsRatio)) return false;
        if (!h5tools->readGroupStr(*_h5Group, "MaterialDescribe", describe)) return false;
        _elasticData->setMaterialDescribe(QString::fromStdString(describe));
        _elasticData->setDensity(density);
        _elasticData->setYoungsModulus(youngsModulus);
        _elasticData->setPoissonsRatio(poissonsRatio);
        if (!readRadiossData(_elasticData, *_h5Group)) return false;
        if (!readDataObject(_elasticData, *_h5Group)) return false;
        return true;
    }

    bool FITKRadiossDataMaterialElasticAdaptor::adaptW()
    {
        _elasticData = dynamic_cast<Radioss::FITKMaterialElastic*>(_dataObj);
        if (_writer == nullptr || _elasticData == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _writer->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        //获取数据
        double density = _elasticData->getDensity();
        double youngsModulus = _elasticData->getYoungsModulus();
        double poissonsRatio = _elasticData->getPoissonsRatio();
        QString type = Radioss::FITKMaterialElastic::GetFITKMaterialElasticRadiossKeyWord();

        int materialID = _elasticData->getMaterialID();
        QString describe = _elasticData->getMaterialDescribe();
        h5tools->writeGroupAttrStr(*_h5Group, "MaterialDescribe", describe.toStdString());
        h5tools->writeGroupAttrInt(*_h5Group, "MaterialID", materialID);
        h5tools->writeGroupAttrDouble(*_h5Group, "Density", density);
        h5tools->writeGroupAttrDouble(*_h5Group, "YoungsModulus", youngsModulus);
        h5tools->writeGroupAttrDouble(*_h5Group, "PoissonsRatio", poissonsRatio);
        h5tools->writeGroupAttrStr(*_h5Group, "MaterialType", type.toStdString());

        if (!writeRadiossData(_elasticData, *_h5Group))return false;
        if (!writeDataObject(_elasticData, *_h5Group))return false;
        return true;
    }
}
